/**
 * 
 */
package com.hydra.spring.springTest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Created on 2013-7-23
 * @Title:
 * @Description:
 * @author:黄海军
 */
public class DynamicProxyHandler implements InvocationHandler {
	Log logger = LogFactory.getLog(this.getClass()); 
	private Object business;// 被代理对象
	private InterceptorClass inceptor = new InterceptorClass();// 拦截器

	/**
	 * @Discription:
	 * @coustructor 方法.
	 */
	public DynamicProxyHandler(Object business) {
		super();
		this.business = business;
	}

	/**
	 * @Created on 2013-7-23
	 * @Discription:
	 * @author:黄海军
	 * @update:黄海军
	 */
	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		Object result = null;
		inceptor.before();
		//调用被代理类的方法
		result = method.invoke(business, args);
		inceptor.after();
		logger.info(result);
		return result;
	}

	public static void main(String[] args) {
		BusinessInterface business = new Business2Class();
		DynamicProxyHandler handler = new DynamicProxyHandler(business);
		//创建代理类对象
		BusinessInterface businessProxy = (BusinessInterface) Proxy
				.newProxyInstance(business.getClass().getClassLoader(),
						business.getClass().getInterfaces(), handler);
		businessProxy.doSomething();
	}

}
